import fs from 'fs';
import path from 'path';
const env = process.env.front_env;

function generateCfg() {
  // 默认配置路径
  const defaultConfigPath = path.join(__dirname, 'development.json');
  // 本地配置路径
  const localConfigPath = path.join(__dirname, 'local.json');
  let localConfig = {};
  let envConfig = {};

  if (fs.existsSync(localConfigPath)) {
    localConfig = require(localConfigPath);
  }
  // 获取
  if (env) {
    const envCfgPath = path.join(__dirname, `${env}.json`);
    if (fs.existsSync(envCfgPath)) {
      console.log('file_exists', envCfgPath);
      envConfig = require(envCfgPath);
    } else {
      console.warn(`\nConfiguration file specified by env var ${env} = ${envCfgPath} does not exist.\n`);
    }
  }

  // 获取默认配置
  const defaultConfig = require(defaultConfigPath);

  // 拼接最终配置
  const mixedConfig = Object.assign(
    {},
    defaultConfig,
    localConfig,
    envConfig
  );
  return mixedConfig;
}
export default generateCfg;
